package com.xxc.hs.db;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.xxc.hs.bean.UserInfo;

public class UserHelper extends SQLiteOpenHelper {
    private static UserHelper helper;
    private static final String DB_NAME = "user.db";
    private static final int VERSION = 1;

    private UserHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    public synchronized  static UserHelper getInstance(Context context){
        if(null == helper){
            helper = new UserHelper(context.getApplicationContext(), DB_NAME,null, VERSION);
            helper.AddAdmin();
        }
        return helper;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table user_table(user_id integer primary key autoincrement, name text,pwd text ,type integer)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

    /**
     * 添加管理员用户
     */
    private void AddAdmin(){
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();

        values.put("name","admin");
        values.put("pwd","admin");
        values.put("type",1);
        String s = "values(null,?,?,?)";
        int ids = (int) db.insert("user_table",s,values);
        db.close();
    }




    /**
     * 登录
     */
    @SuppressLint("Range")
    public UserInfo login(String username){
        SQLiteDatabase db = getReadableDatabase();
        UserInfo userInfo = null;
        String sql = "select * from user_table where name = ?";
        String[] s = {username};
        Cursor cursor = db.rawQuery(sql,s);
        if(cursor.moveToNext()){
            int _id = cursor.getInt(cursor.getColumnIndex("user_id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String pwd = cursor.getString(cursor.getColumnIndex("pwd"));
            int type = cursor.getInt(cursor.getColumnIndex("type"));
            userInfo = new UserInfo(_id, name, pwd, type);
        }
        cursor.close();
        db.close();
        return userInfo;
    }


    /**
     * 注册
     */
    public int register(String username,String password,int type){
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();

        values.put("name",username);
        values.put("pwd",password);
        values.put("type",type);
        String s = "values(null,?,?,?)";
        int ids = (int) db.insert("user_table",s,values);
        db.close();
        return ids;
    }


    /**
     * 修改密码
     */
    public int updatePwd(String name,String pwd){
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("pwd",pwd);
        int ids = db.update("user_table",values,"name = ?",new String[]{name});
        db.close();
        return ids;
    }


    /**
     * 修改类型
     */
    public int updateType(String name,int type){
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("type",type);
        int ids = db.update("user_table",values,"name = ?",new String[]{name});
        db.close();
        return ids;
    }

}
